/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vng.survey.controller;


import com.vng.zing.survey.thrift.client.SurveyClient;
import com.vng.zing.survey.thrift.model.QuesType;
import com.vng.zing.survey.thrift.model.Question;
import com.vng.zing.survey.thrift.model.SurveyItem;
import com.vng.zing.survey.thrift.model.SurveyItemResult;
import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import vng.survey.model.CommonModel;
import vng.survey.model.RenderModel;


/**
 *
 * @author namhh
 */
public class EditSurveyController extends BaseController {
    
    @Override
   public String render(HttpServletRequest req, HttpServletResponse resp) throws IOException, Exception {
         if(req.getParameter("id")== null)
               return showError("Không tìm thấy khảo sát yêu cầu!");
        int idSurvey = Integer.parseInt(req.getParameter("id"));
        String content = "";
         
        TemplateDataDictionary dic = TemplateDictionary.create();
        dic.setVariable("HEADER", CommonModel.getHeader(TemplateDictionary.create(),"Chỉnh sữa khảo sát"));
            dic.setVariable("FOOTER", CommonModel.getFooter(TemplateDictionary.create()));
       
             SurveyClient surveyClient = SurveyClient.getInstance();
        SurveyItemResult itemResult = surveyClient.getSurvey(idSurvey);
        if(itemResult.getErro()!= com.vng.zing.survey.thrift.model.Error.SUCCESS || itemResult.getSurveyItem().getIDUser()!= 1){
             content = CommonModel.getError(dic, "Khảo sát không tồn tại!");
            return content;
        
        }

        Map<Short,Integer> map =  surveyClient.listQuesCanEdit(idSurvey);
        String responsed = (map.isEmpty())?"false":"true";
        SurveyItem survey = itemResult.getSurveyItem();
        

        
            
            
            dic.setVariable("ID_SURVEY", String.format("%s", idSurvey));
            dic.setVariable("TITLE", survey.getTitle());
            dic.setVariable("DESCRIPTION", survey.getDescription());
            dic.setVariable("RESPONSE",responsed);
            String date = CommonModel.getDate(survey.getBeginDay());
            dic.setVariable("START_DATE", date);
             date = CommonModel.getDate(survey.getEndDay());
             dic.setVariable("END_DATE", date);
            dic.setVariable("POINT",String.format("%s",survey.getPoint()));
            int index = 1;
            for (Question question : survey.getQuestions()) {
                TemplateDataDictionary sliceQuestions = dic.addSection("SLICE_QUESTION");
                sliceQuestions.setVariable("ID_QUESTION", String.format("%s", question.getIdQues()));
                sliceQuestions.setVariable("INDEX", String.format("%s", index));
                sliceQuestions.setVariable("TITLE", question.getTitleQues());
                sliceQuestions.setVariable("HELP", question.getHelpText());
                if(map.isEmpty())
                    sliceQuestions.setVariable("EDIT","false");
                else{
                    Integer isEdit = map.get(question.getIdQues());
                    if(isEdit==null){
                        sliceQuestions.setVariable("EDIT","false");
                    }else{
                        sliceQuestions.setVariable("EDIT","true");
                    }      
                }
               if(question.isRequire())
                    sliceQuestions.setVariable("REQUIRE", "(*)");
               else
                    sliceQuestions.setVariable("REQUIRE", "");
               sliceQuestions.setVariable("REQUIRE_VALUE", String.format("%s", question.isRequire()));
                if (question.getType() == QuesType.SINGLE_LINE) {
                    sliceQuestions.setVariable("ANSWER", CommonModel.getTextAnswer(TemplateDictionary.create()));
                }
                if (question.getType() == QuesType.MULTI_LINE) {
                    String content1 = CommonModel.getParagraphAnswer(TemplateDictionary.create());
                    sliceQuestions.setVariable("ANSWER", content1);
                }
                if (question.getType() == QuesType.SINGLE_SELECTION) {

                    TemplateDataDictionary dicAnswers = TemplateDictionary.create();

                    for (String answer : question.getChoices()) {
                        TemplateDataDictionary sliceAnswers = dicAnswers.addSection("SLICE_OPTION");
                        sliceAnswers.setVariable("OPTION", answer);
                        sliceAnswers.setVariable("RADIO_NAME", String.format("%s", question.getIdQues()));
                    }
                    content = CommonModel.getSingleChoiceAnswer(dicAnswers);
                    sliceQuestions.setVariable("ANSWER", content);

                }

                if (question.getType() == QuesType.MULTI_CHOICE) {

                    TemplateDataDictionary dicAnswers = TemplateDictionary.create();

                    for (String answer : question.getChoices()) {
                        TemplateDataDictionary sliceAnswers = dicAnswers.addSection("SLICE_OPTION");
                        sliceAnswers.setVariable("OPTION", answer);
                    }
                    content = CommonModel.getMultipleChoiceAnswer(dicAnswers);
                    sliceQuestions.setVariable("ANSWER", content);

                }

                index++;
            }

            Template template = RenderModel.getCTemplate("edit_survey");
            content = template.renderToString(dic);
       
        return content;
    }

 
   
}
